// 引入JSEncrypt
import JSEncrypt from 'jsencrypt';

// 公钥字符串
const publicKey =
  'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZCXB0ScO6bx4adsn8lMeepIQJCq9vSJPEGquG4j1RNTP4+TPIvmJam1C+f/Ol94wA9/Uki/l1r2Za2fk4mgZw+ggbwbGOGnWRJ8/8V65K9M43PB8dy7idapiVMZQFnUXrcqbUjVtOG88EtPHc3gPRhsD3jatoDQcJQ99d03ZTlQIDAQAB';
const privateKey =
  'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANJ+Qc77gMuByihf+AQhPgrs3QyS+tkkQ+lO2p8lUqAIhoDVi/uAy9837fs8mNdDh+4qiErmD7z4lLnyy+vreyepnJTjP8BmY7qKVjjPGRop9606mN+qdI1iUtoGKwqu34y5agvX0Fw6xqfZFwpqvypVQdlYy/ItBfMRd2uycawdAgMBAAECgYBqK4TGAdVU0IJHGyDd9GZ23EIitYARRll6tGtRGmIylhpBoXSZxr0OOgevUUFGUqmdV5anMKN/jWJ26CAzPvM+g5md2zmA8a5JcmuvlVyp/b73apR3znRdj8ZPEPa8oU0ajV3HxBmX4oNQ4J1m3Z4jc5LkJb7mcH9MoQBuZuxYAQJBAPY9TpnFEncQRT8KiJg8ZN0uXE3uyF3KEG19SNd/Z3hxHL7ui9BAlAzpRaYkIunEQICVASCyglUsiVuXFZdom00CQQDa1jfAgClDd62LUV9qj+NwJ9H3Oo185Z/eATFXw5IssOLDRwvJWQteeZvpPDOfq9mVzZ8HgK8qk8wX6aJgtswRAkAGaeRhNvPbLA5RrQmPxYrvA+Udlombf8bbwzahfk0IoxGXm4kCvJveljjdMfXC1vzDHJ5x5pRkeYt+WjoSvoPFAkAES0vQxTMKFeEb6L7cknI+Eyn1lfqMxty6OH/FXxsRMW9hsZUzoEGu2ugKTZodufuNCvnh5c6KIVhR58295BhhAkEAkZrWooHS5GF04gcYpXWRdt0B6XlGqEt3ZXtdbdkf5fkPncb6QFe/feJS4IPRz+LttuSvpPKf41fiEm1bRuzwgQ==';
// 将公钥字符串转换为 PEM 格式
const pemPublicKey = `-----BEGIN PUBLIC KEY-----\n${publicKey.replace(
  /\n/g,
  '\n' + ' '.repeat(24),
)}\n-----END PUBLIC KEY-----`;

const pemPrivateKey = `-----BEGIN PUBLIC KEY-----\n${privateKey.replace(
  /\n/g,
  '\n' + ' '.repeat(24),
)}\n-----END PUBLIC KEY-----`;

// RSA 加密方法
export function encryptRSA(data: string): string {
  // 创建JSEncrypt实例
  const encryptor = new JSEncrypt();

  // 设置公钥
  encryptor.setPublicKey(pemPublicKey);

  encryptor.setPrivateKey(privateKey); // 设置私钥

  // 使用公钥进行加密
  const encrypted = encryptor.encrypt(data);
  console.log(encrypted);
  console.log(encryptor.decrypt(encrypted.toString())); // 对数据进行解密
  // 返回 Base64 编码的加密结果
  return encrypted.toString();
}

// RSA 解密方法
export function decryptRSA(data: string): string {
  // 创建JSEncrypt实例
  const encryptor = new JSEncrypt();

  encryptor.setPrivateKey(pemPrivateKey); // 设置私钥
  // 对数据进行解密
  const encrypted = encryptor.decrypt(data);
  // 返回 Base64 编码的加密结果
  return encrypted.toString();
}

function loadKey() {}
